/*
 *  POJ2676: Sudoku
 */

/*-------- Includes --------*/
#include <iostream>
#include <cstring>

using namespace std;

/*-------- Types --------*/

/*-------- Global Variables --------*/
char sudo[9][10];  // sudoku storage
bool rowm[9][9];    // row mark
bool colm[9][9];    // column mark
bool blkm[9][9];    // block mark

/*-------- Function Prototypes --------*/
bool dfs(int pos);

/*-------- Main Function --------*/
int main(int argc, char const *argv[])
{
    int nCase;

    cin >> nCase;

    while (nCase--) {
        for (int i = 0; i < 9; i++)
            cin >> sudo[i];

        // initialize
        memset(rowm, 0, sizeof(rowm));
        memset(colm, 0, sizeof(colm));
        memset(blkm, 0, sizeof(blkm));

        for (int i = 0; i < 9; i++) {
            for (int j = 0; j < 9; j++) {
                int n = sudo[i][j] - '0';
                if (n) {
                    int k = i / 3 * 3 + j / 3;  // better solution?

                    rowm[i][n-1] = true;
                    colm[j][n-1] = true;
                    blkm[k][n-1] = true;
                }
            }
        }

        // depth first search
        if (dfs(0))
            for (int i = 0; i < 9; i++)
                cout << sudo[i] << endl;
    }

    return 0;
}

/*-------- Functions --------*/
bool dfs(int pos)
{
    // base case: reach the bottom
    if (pos == 81) return true;

    int r = pos / 9;
    int c = pos % 9;
    int b = r / 3 * 3 + c / 3;

    // cut off
    if (sudo[r][c] != '0') return dfs(pos + 1);

    for (int i = 0; i < 9; i++) {
        // cut off
        if (rowm[r][i] || colm[c][i] || blkm[b][i])
            continue;

        rowm[r][i] = true;
        colm[c][i] = true;
        blkm[b][i] = true;

        sudo[r][c] = i + '1';

        if (dfs(pos + 1)) return true;

        // revert for backtracing
        rowm[r][i] = false;
        colm[c][i] = false;
        blkm[b][i] = false;
    }

    // revert for backtracing
    sudo[r][c] = '0';

    return false;
}
